
#include	"main.h"
#include	"BC26103.h"
#include "Network communication.h"
#include "TelecomPlatform.h"
extern uint8_t RcvFromNB_Valid_Data[4];
extern uint16_t up_adc_data;
extern uint8_t Operators;
uint8_t	SendMessageToServer_Mobile(uint8_t	socketid,measure_value*value)
{
	uint8_t i=0;
	uint8_t	SendDataBuf[60]={0};
	for(i=0;i<IMEI_Len;i++)
		SendDataBuf[i] = BC26_IMEI[i];
	for(i=0;i<ICCID_Len;i++)
		SendDataBuf[IMEI_Len+i] = BC26_ICCID[i];
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len,"%s","061010");
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len+6,"%04X",MeasureData.up_data);
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len+10,"%02X",MeasureData.AlarmFlag);
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len+12,"%02X",value->SignalStrength);
	SendDataBuf[IMEI_Len+ICCID_Len+14]=0;
	printf("-->>send:%s\r\n",SendDataBuf);
	return	SendDataToServer(socketid,SendDataBuf,IMEI_Len+ICCID_Len+14);
}
uint8_t	SendMessageToServer_Telecom(measure_value*value)
{
	uint8_t i=0;
	uint8_t	SendDataBuf[60]={0};
	for(i=0;i<IMEI_Len;i++)
		SendDataBuf[i] = BC26_IMEI[i];
	for(i=0;i<ICCID_Len;i++)
		SendDataBuf[IMEI_Len+i] = BC26_ICCID[i];
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len,"%s","061010");
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len+6,"%04X",MeasureData.up_data);
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len+10,"%02X",MeasureData.AlarmFlag);
	sprintf(SendDataBuf+IMEI_Len+ICCID_Len+12,"%02X",value->SignalStrength);
	SendDataBuf[IMEI_Len+ICCID_Len+14]=0;
	printf("-->>send:%s\r\n",SendDataBuf);
	return SendDataToTelecomPlatform(SendDataBuf,IMEI_Len+ICCID_Len+14);
}
#if 1
uint8_t	ReceiveMessageFromServer_Mobile(uint8_t	socketid)
{
	uint8_t i;
	uint8_t RS485_TX_len = 0;
	uint8_t	buffer[ReceiveUdpDataMaxLen]={0},len;
	uint8_t RcvFromNB_Valid_len;
		len=ReceiveDataFromServer(socketid,buffer);
		if(len>=27)
		{
			for(i=0;i<IMEI_Len;i++)
			{
				if(buffer[i]!=BC26_IMEI[i])
				{
					printf("Recv IMEI incorrect...\r\n");
					return	FALSE;
				}
			}
			RcvFromNB_Valid_len = ((buffer[IMEI_Len]-'0')<<4) + (buffer[IMEI_Len+1]-'0');
			printf("receive from NB valid data len = %d\r\n",RcvFromNB_Valid_len);
			if(RcvFromNB_Valid_len==0x04)
			{
					for(i=0;i<RcvFromNB_Valid_len;i++)
					{
						RcvFromNB_Valid_Data[i] = ((buffer[IMEI_Len+2+i+i]-'0')<<4) + (buffer[IMEI_Len+3+i+i]-'0');
						printf("%x  ",RcvFromNB_Valid_Data[i]);
					}
					printf("\r\n");
					if(RcvFromNB_Valid_Data[0]==0x01)
					{
						Write_Config();
						Read_Config();
					}
					return	TRUE;
			}
		}
		return	FALSE;
}
uint8_t	ReceiveMessageFromServer_Telecom(void)
{
	uint8_t	buffer[ReceiveUdpDataMaxLen]={0},len;
	uint8_t RcvFromNB_Valid_len;
	uint8_t i=0;
	len=ReceiveMessageFromTelecomPlatform(buffer);
	if(len>=27)
	{
		for(i=0;i<IMEI_Len;i++)
		{
			if(buffer[i]!=BC26_IMEI[i])
			{
				printf("Recv IMEI incorrect...\r\n");
				return	FALSE;
			}
		}
		RcvFromNB_Valid_len = ((buffer[IMEI_Len]-'0')<<4) + (buffer[IMEI_Len+1]-'0');
		if(RcvFromNB_Valid_len==0x04)
		{
				for(i=0;i<RcvFromNB_Valid_len;i++)
				{
					RcvFromNB_Valid_Data[i] = ((buffer[IMEI_Len+2+i+i]-'0')<<4) + (buffer[IMEI_Len+3+i+i]-'0');
					printf("%x  ",RcvFromNB_Valid_Data[i]);
				}
				printf("\r\n");
				if(RcvFromNB_Valid_Data[0]==0x01)
				{
					Write_Config();
					Read_Config();
				}
			return	TRUE;
		}
	}
	return	FALSE;
}
#endif